﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Net;
using System.Text.RegularExpressions;
using GalaSoft.MvvmLight;
using MvvmLight1.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace MvvmLight1.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private readonly IDataService _dataService;

        /// <summary>
        /// The <see cref="WelcomeTitle" /> property's name.
        /// </summary>
        public const string WelcomeTitlePropertyName = "WelcomeTitle";

        private string _welcomeTitle = string.Empty;

        /// <summary>
        /// Gets the WelcomeTitle property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string WelcomeTitle
        {
            get
            {
                return _welcomeTitle;
            }

            set
            {
                if (_welcomeTitle == value)
                {
                    return;
                }

                _welcomeTitle = value;
                RaisePropertyChanged(WelcomeTitlePropertyName);
            }
        }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IDataService dataService)
        {
            _dataService = dataService;
            _dataService.GetData(
                (item, error) =>
                {
                    if (error != null)
                    {
                        // Report error here
                        return;
                    }

                    WelcomeTitle = item.Title;
                });

            WebClient client = new WebClient();
            //client.QueryString["count"]
            string json = client.DownloadString(
                @"https://api.optify.net/v1/sites/H9WOLEF1/visitors.json?access_token=doxv1w08ukw2f16mquk61xxu&count=1");
            try
            {
                json = Regex.Replace(json, @"\b\d{13}\b", @"new Date($0)");
                JsonConvert.SerializeObject(DateTime.Now, new JavaScriptDateTimeConverter());
                //JsonConvert.SerializeObject(DateTime.Now, new 
                List<Visitor> visitors = JsonConvert.DeserializeObject<List<Visitor>>(json, new JavaScriptDateTimeConverter());
                foreach (Visitor visitor in visitors)
                {
                    foreach (var visit in visitor.Visits)
                    {
                        visit.Visitor = visitor;
                        visit.Visitor_Id = visitor.Visitor_Id;
                        foreach(Pageview pageview in visit.Pageviews)
                        {
                            pageview.Visit = visit;
                            pageview.Visit_Id = visit.Visit_Id;
                            pageview.Visitor_Id = visit.Visitor_Id;
                        }
                        foreach (Form form in visit.Forms)
                        {
                            form.Visit = visit;
                            form.Visit_Id = visit.Visit_Id;
                            form.Visitor_Id = visit.Visitor_Id;
                            MSSPWHEntities db = new MSSPWHEntities();
                            db.Entry(form).State = EntityState.Modified;
                        }
                    }
                }
            }
            catch (Exception er)
            {
                
                //throw;
            }
           
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
    }
}